<!--
 * @Author: zl
 * @Date: 2021-02-05 14:52:13
 * @LastEditTime: 2021-11-30 18:49:18
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3-element-admin\src\views\layoutpages\system\Roles.vue
-->
<template>
    <div class="ve_container">
        <!-- 搜索 -->
        <el-form ref="queryForm" :inline="true" :model="params">
            <el-form-item label="村庄名称" prop="villageCode">
                <el-select
                    style="width: 100%"
                    v-model="villageCode"
                    placeholder="村庄名称"
                    clearable
                >
                    <el-option
                        v-for="item in villageType"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                        :disabled="item.villageCode == 0"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="居民类型" prop="villageCode">
                <el-select
                    style="width: 100%"
                    v-model="type"
                    placeholder="居民类型"
                    clearable
                >
                    <el-option
                        v-for="item in jmType"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                        :disabled="item.type == 0"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
                <el-input
                    clearable
                    v-model="name"
                    placeholder="请输入"
                ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    @click="onSubmit(params, getDataList)"
                >
                    {{ menus.search.name }}
                </el-button>
                <!--                <el-button @click="resetForm(queryForm, params, getDataList)">-->
                <!--                    重置-->
                <!--                </el-button>-->
            </el-form-item>
        </el-form>

        <!-- 列表 -->
        <ve-table
            :table="{
                data: tableData,
            }"
            :pagination="{
                onSizeChange: (val) =>
                    handleSizeChange(val, params, getDataList),
                onCurrentChange: (val) =>
                    handleCurrentChange(val, params, getDataList),
                currentPage: page,
                pageSize: pageSize,
                total: total,
            }"
        >
            <template #tool_bar>
                <el-button
                    v-permission="['add']"
                    size="mini"
                    type="primary"
                    @click="handleEdit(menus.add.name)"
                >
                    {{ menus.add.name }}
                </el-button>
            </template>
            <el-table-column prop="villageCode" label="村庄名称">
                <template v-slot="{ row }">
                    <el-tag>
                        {{
                            villageType.filter(
                                (item) => item.code == row.villageCode
                            )[0].name
                        }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="姓名"></el-table-column>
            <el-table-column prop="phone" label="手机号"></el-table-column>
            <el-table-column prop="type" label="居民类型">
                <template v-slot="{ row }">
                    <el-tag>
                        {{
                            jmType.filter((item) => item.code == row.type)[0]
                                .name
                        }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作">
                <template v-slot:default="{ row }">
                    <el-button
                        v-permission="['edit']"
                        @click.prevent="handleEdit(menus.edit.name, row)"
                        type="primary"
                        size="mini"
                    >
                        {{ menus.edit.name }}
                    </el-button>
                    <el-button
                        v-permission="['del']"
                        @click.prevent="handleDel(row.id)"
                        type="danger"
                        size="mini"
                    >
                        {{ menus.del.name }}
                    </el-button>
                </template>
            </el-table-column>
        </ve-table>
        <!-- 编辑组件 -->
        <address-book-edit
            v-if="showDialog"
            :rowData="rowData"
            :title="dialogTitle"
            :showDialog="showDialog"
            @closeDialog="handelDialog($event)"
        />
    </div>
</template>
<script>
export default {
    data: () => ({
        description: "通讯录",
        menus: {
            search: { name: "查询" },
            add: { name: "添加" },
            edit: { name: "编辑" },
            del: { name: "删除" },
        },
    }),
};
</script>

<script setup>
import AddressBookEdit from "./components/AddressBookEdit";
import { reactive, toRefs, ref, onMounted, getCurrentInstance } from "vue";
//*导入公共查询方法
import {
    onSubmit,
    handleSizeChange,
    handleCurrentChange,
} from "@/views/layoutpages/common";

const { proxy } = getCurrentInstance();

const rowData = ref(null);
const dialogTitle = ref("");
const showDialog = ref(false);
const queryForm = ref(null);
const tableData = ref([]);
const villageType = ref([]);
const jmType = ref([]);
const params = reactive({
    pageSize: 10,
    page: 1,
    total: 0,
    name: "",
    villageCode: "",
    type: "",
});
const { name, pageSize, page, total, villageCode, type } = toRefs(params);

/**
 * @description:添加or编辑事件
 * @param {*}
 * @return {*}
 */
const handleEdit = (title, row = null) => {
    showDialog.value = true;
    dialogTitle.value = title;
    rowData.value = row;
};

/**
 * @description: 获取村庄信息
 * @param {*}
 * @return {*}
 */
const getVillageList = async () => {
    const { code, data } = await VE_API.system.dictionaryEntityByCode({
        dicCode: "czxx",
    });
    if (code == "00") {
        villageType.value = data;
    }
};
const getJmTypeList = async () => {
    const { code, data } = await VE_API.system.dictionaryEntityByCode({
        dicCode: "jmlx",
    });
    if (code == "00") {
        jmType.value = data;
    }
};

/**
 * @description: dialog事件
 * @param {*}
 * @return {*}
 */
const handelDialog = (e) => {
    showDialog.value = e;
    getDataList();
};
/**删除行数据
 * @description:
 * @param {*}
 * @return {*}
 */
const handleDel = (id) => {
    proxy
        .$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "error",
        })
        .then(async () => {
            const { code } = await VE_API.system.villagePhoneDel([id]);
            if (code == "00") {
                getDataList();
            }
        })
        .catch(() => {
            proxy.$message({
                type: "info",
                message: "已取消删除",
            });
        });
};

/**
 * @description: 获取列表数据
 * @param {*}
 * @return {*}
 */
const getDataList = async () => {
    const { code, data } = await VE_API.system.villagePhoneList(params);
    if (code == "00") {
        const { pageSize, pageNum, total, list } = data;
        params.pageSize = pageSize;
        params.page = pageNum;
        params.total = total;
        tableData.value = list;
    }
};
onMounted(async () => {
    await getVillageList();
    await getJmTypeList();
    await getDataList();
    // maxHeight(pagination, queryForm, toolBar, ve_max_height);
});
</script>

<style lang="scss" scoped></style>
